
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Inscription
 */
@WebServlet("/Inscription")
public class Inscription extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Inscription() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Connection connection = null;
		
		
		String nom = request.getParameter("nom");
		String prenom = request.getParameter("prenom");
		String mdp = request.getParameter("mdp");		
		String conf_mdp = request.getParameter("conf_mdp");
		String email = request.getParameter("email");
		
		
		
		if(!nom.equals("") && !prenom.equals("") && !mdp.equals("") && !conf_mdp.equals("") && !email.equals("")) {
			
			try {
				Class.forName( "com.mysql.jdbc.Driver" );
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			try {
				connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1/IngeWeb", "root", "");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				System.out.println("Connection ratée.");
				e.printStackTrace();
			}
			
			
			
			if(nom.length() >= 3 && prenom.length() >= 3 && mdp.length() >= 3 && email.length() >= 7){
			
				HttpSession session = request.getSession();
				int insertion = 0, newId = 0;
			
				
				Statement stmt = null;
				try {
					stmt = connection.createStatement();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
			
			
				
			
			
				try {
					insertion = stmt.executeUpdate("INSERT INTO Utilisateur( nom_utilisateur, prenom_utilisateur, email_utilisateur, password_utilisateur) VALUES ('"+nom+"', '"+prenom+"','"+email+"' , '"+mdp+"' );");
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				if(insertion == 1){
					
					System.out.println("L'insertion des paramétres : '"+nom+"', '"+prenom+"', '"+mdp+"', '"+email+"', est r�ussie.");
										
					session.setAttribute("nom", nom);
					session.setAttribute("prenom", prenom);
					session.setAttribute("email", email);
					request.getRequestDispatcher("/accueil.jsp");
					
				}
	
				else
					System.out.println("L'insertion des paramétres : '"+nom+"', '"+prenom+"', '"+mdp+"', '"+email+"', est rat�e.");
			}
			
	
			
		}
		else{
			request.getRequestDispatcher("erreur.jsp");
			System.out.println("Dans erreur");
		}
		
	}

}
